package com.springboot.controller;

import com.springboot.domain.BtDemand;
import com.springboot.domain.BtUser;
import com.springboot.domain.Result;
import com.springboot.dto.EditTeamDTO;
import com.springboot.dto.TeamDemandDetailDTO;
import com.springboot.enums.ResultEnum;
import com.springboot.repository.UserRepository;
import com.springboot.service.TeamService;
import com.springboot.tools.ResultUtil;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/bank")
public class TeamController {
    private TeamService teamService;
    private UserRepository userRepository;
    @Autowired
    public TeamController(TeamService teamService,UserRepository userRepository) {
        this.teamService=teamService;
        this.userRepository=userRepository;
    }

    //团队管理  user模糊查询
    @ResponseBody
    @GetMapping(value = "/team/userFuzzyQuery")
    public Result personFuzzyQuery(@RequestParam(value = "name", defaultValue = "",required = false) String name) {
        List<BtUser> users=userRepository.findByFuzzyQuery(name);
        return ResultUtil.success(users);
    }

    @ResponseBody
    @PostMapping(value = "/team/edit")
    public Result editTeam(@RequestBody EditTeamDTO editTeamDTO, HttpSession session){
        String userId = (String) session.getAttribute("user");
        if( userId == null){
            return ResultUtil.error(ResultEnum.NOT_LOGIN);
        }
       boolean flag = teamService.editTeam(editTeamDTO,userId);
        if (flag){
            return ResultUtil.success("成员修改成功");
        }else {
            return ResultUtil.error(ResultEnum.TEAM_FAILED);
        }

    }

    //个人团队列表
    @ResponseBody
    @GetMapping(value = "/team/teamDemandList")
    public Result demandSubList(@ApiParam Pageable pageable,HttpSession session) {
        Page<BtDemand> btDemandSub=teamService.findDemandByTeam(pageable,session);
        return ResultUtil.success(btDemandSub);
    }

   // 团队详情
    @ResponseBody
    @GetMapping(value = "/team/detail/{id}")
    public Result taskDetail(@PathVariable("id") String id){
        TeamDemandDetailDTO teamDemandDetailDTO=teamService.findDemandTeamDetail(id);
        return ResultUtil.success(teamDemandDetailDTO);
    }
}
